package com.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.bean.PurchaseOrderMaterial;
import com.bean.Stock;
import com.bean.StockApply;
import com.bean.StockApplyMaterial;
import com.bean.User;
import com.service.StockApplyMaterialService;
import com.service.StockApplyService;
import com.service.StockService;
import com.util.TimeUtil;

/**
 * @author author : 张鹏
 * @date createTime：2018年3月2日 下午3:14:06
 * @Description: 该类的功能描述
 */
@Controller
@RequestMapping("stockApply")
public class StockApplyController {
	@Autowired
	private StockApplyService stockApplyService;
	@Autowired
	private StockApplyMaterialService stockApplyMaterialService;
	@Autowired
	private StockService stockService;

	private StockApply stockApply;

	@RequestMapping("getStockApplyByCategoryAjax")
	@ResponseBody
	public List<StockApply> getStockApplyByCategoryAjax(String category) {
		return stockApplyService.getStockApplyByCategory(category);
	}

	@RequestMapping("getStockApplyByFinishAjax")
	@ResponseBody
	public List<StockApply> getStockApplyByFinishAjax(int finish) {
		return stockApplyService.getStockApplyByFinish(finish);
	}

	@RequestMapping("getStockApplyByStateAjax")
	@ResponseBody
	public List<StockApply> getStockApplyByStateAjax(int state) {
		return stockApplyService.getStockApplyByState(state);
	}

	@RequestMapping("getStockApplyByStateAndCategoryAjax")
	@ResponseBody
	public List<StockApply> getStockApplyByStateAndCategoryAjax(int state,
			String category) {
		stockApply = new StockApply();
		stockApply.setState(state);
		stockApply.setCategory(category);
		return stockApplyService.getStockApplyByStateAndCategory(stockApply);
	}

	@RequestMapping("getStockApplyByFinishAndCategoryAjax")
	@ResponseBody
	public List<StockApply> getStockApplyByFinishAndCategoryAjax(int finish,
			String category) {
		stockApply = new StockApply();
		stockApply.setFinish(finish);
		stockApply.setCategory(category);
		return stockApplyService.getStockApplyByFinishAndCategory(stockApply);
	}

	@RequestMapping("getStockApplyByPrimaryKeyAjax")
	@ResponseBody
	public StockApply getStockApplyByPrimaryKeyAjax(int stockApplyId) {
		return stockApplyService.getStockApplyByPrimaryKey(stockApplyId);
	}

	/**
	 * 根据主键查询入库或出库申请
	 * 
	 * @param purchaseOrderId
	 * @param remark
	 *            show表示查看 exame表示审核
	 * @param returnTo
	 *            表示返回的jsp
	 * @param request
	 * @return
	 */
	@RequestMapping("getStockApplyByPrimaryKeyReturnTo")
	public String getStockApplyByPrimaryKeyReturnTo(int stockApplyId,
			String remark, String returnTo, HttpServletRequest request) {
		request.setAttribute("stockApply",
				stockApplyService.getStockApplyByPrimaryKey(stockApplyId));
		request.setAttribute("stockApplyMaterialList",
				stockApplyMaterialService
						.getMaterialByStockApplyId(stockApplyId));
		request.setAttribute("remark", remark);
		return returnTo;
	}

	@RequestMapping("deleteStockApplyByPrimaryKeyAjax")
	@ResponseBody
	public int deleteStockApplyByPrimaryKeyAjax(int stockApplyId) {
		stockApply.setStockApplyId(stockApplyId);
		stockApply.setFinish(-1);
		return stockApplyService.deleteStockApplyByFinish(stockApply);
	}

	/**
	 * 创建入库申请
	 * 
	 * @param remark
	 * @param category
	 * @param materialList
	 * @return
	 */
	@RequestMapping("createNewInStockApplyAjax")
	@ResponseBody
	public int createNewInStockApplyAjax(String remark, String category,
			String materialList,HttpServletRequest request) {
		int userId=((User)request.getSession().getAttribute("userInfo")).getUserId();
		stockApply = new StockApply();
		stockApply.setCreateById(userId);// 暂时设置为1
		stockApply.setCreateTime(TimeUtil.formatTime());
		stockApply.setState(1);
		stockApply.setFinish(0);
		stockApply.setCategory(category);
		stockApply.setRemark(remark);
		int result = stockApplyService.createNewStockApply(stockApply);
		int stockApplyId = stockApply.getStockApplyId();

		// 处理入库申请的商品表
		if (!"".equals(materialList) && materialList != null
				&& !"[]".equals(materialList)) {
			StockApplyMaterial material;
			JSONArray list = JSONArray.fromObject(materialList);
			JSONObject jobj;
			for (int i = 0; i < list.size(); i++) {
				material = new StockApplyMaterial();
				jobj = (JSONObject) list.get(i);

				material.setStockApplyId(stockApplyId);
				material.setMaterialId(jobj.getInt("materialId"));
				material.setQuantity(jobj.getDouble("materialQuantity"));
				stockApplyMaterialService.createStockApplyMaterial(material);
			}
		}
		return result;
	}
	
	/**
	 * 填写出库申请
	 * @param remark
	 * @param category
	 * @param materialList
	 * @return
	 */
	@RequestMapping("createNewOutStockApplyAjax")
	@ResponseBody
	public int createNewOutStockApplyAjax(String remark, String category,
			String materialList,HttpServletRequest request) {
		int userId=((User)request.getSession().getAttribute("userInfo")).getUserId();
		stockApply = new StockApply();
		stockApply.setCreateById(userId);// 暂时设置为1
		stockApply.setCreateTime(TimeUtil.formatTime());
		stockApply.setState(1);
		stockApply.setFinish(0);
		stockApply.setCategory(category);
		stockApply.setRemark(remark);
		int result = stockApplyService.createNewStockApply(stockApply);
		int stockApplyId = stockApply.getStockApplyId();

		// 处理入库申请的商品表
		if (!"".equals(materialList) && materialList != null
				&& !"[]".equals(materialList)) {
			StockApplyMaterial material;
			JSONArray list = JSONArray.fromObject(materialList);
			JSONObject jobj;
			for (int i = 0; i < list.size(); i++) {
				material = new StockApplyMaterial();
				jobj = (JSONObject) list.get(i);

				material.setStockApplyId(stockApplyId);
				material.setMaterialId(jobj.getInt("materialId"));
				material.setQuantity(jobj.getDouble("materialQuantity"));
				stockApplyMaterialService.createStockApplyMaterial(material);
			}
		}
		return result;
	}

	/**
	 * 审核出库申请
	 * 
	 * @param stockApplyId
	 * @param exameSuggestion
	 * @param state
	 * @param finish
	 * @return
	 */
	@RequestMapping("exameOutStockApplyByPrimaryKeyAjax")
	@ResponseBody
	public int exameOutStockApplyByPrimaryKeyAjax(int stockApplyId,
			String exameSuggestion, int state, int finish,HttpServletRequest request) {
		int userId=((User)request.getSession().getAttribute("userInfo")).getUserId();
		stockApply = new StockApply();
		stockApply.setStockApplyId(stockApplyId);
		stockApply.setExameById(userId);// 暂时设置为1
		stockApply.setExameTime(TimeUtil.formatTime());
		stockApply.setExameSuggestion(exameSuggestion);
		stockApply.setState(state);
		stockApply.setFinish(finish);

		// 审核通过 修改库存数量
		if (state == 2) {
			List<StockApplyMaterial> list = stockApplyMaterialService
					.getMaterialByStockApplyId(stockApplyId);
			Stock stock = null;
			for (StockApplyMaterial material : list) {
				stock = new Stock();
				stock.setCreateById(1);
				stock.setCreateTime(TimeUtil.formatTime());
				stock.setMaterialId(material.getMaterialId());
				stock.setQuantity(0 - material.getQuantity());
				stock.setState(1);
				stockService.insert(stock);
			}
		}

		return stockApplyService.exameStockApply(stockApply);
	}

	/**
	 * 审核出库申请
	 * @param stockApplyId
	 * @param exameSuggestion
	 * @param state
	 * @param finish
	 * @return
	 */
	@RequestMapping("exameInStockApplyByPrimaryKeyAjax")
	@ResponseBody
	public int exameInStockApplyByPrimaryKeyAjax(int stockApplyId,
			String exameSuggestion, int state, int finish,HttpServletRequest request) {
		int userId=((User)request.getSession().getAttribute("userInfo")).getUserId();
		stockApply = new StockApply();
		stockApply.setStockApplyId(stockApplyId);
		stockApply.setExameById(userId);// 暂时设置为1
		stockApply.setExameTime(TimeUtil.formatTime());
		stockApply.setExameSuggestion(exameSuggestion);
		stockApply.setState(state);
		stockApply.setFinish(finish);

		// 审核通过 修改库存数量
		if (state == 2) {
			List<StockApplyMaterial> list = stockApplyMaterialService
					.getMaterialByStockApplyId(stockApplyId);
			Stock stock = null;
			for (StockApplyMaterial material : list) {
				stock = new Stock();
				stock.setCreateById(1);
				stock.setCreateTime(TimeUtil.formatTime());
				stock.setMaterialId(material.getMaterialId());
				stock.setQuantity(material.getQuantity());
				stock.setState(1);
				stockService.insert(stock);
			}
		}

		return stockApplyService.exameStockApply(stockApply);
	}

}
